<?php
class NavigationController extends AdminController{
    public function listAction(){
        $model = new NavigationModel;
        // var_dump($model);
        // $data = $model->getNavigation();        
        $data = $model->get_admin_nav();        
        $this->_view->nav = $data;
    }
    public function addAction(){
        $model = new NavigationModel;
        $this->_view->nav = $model->select();
    }
    public function insertAction(){
        Yaf_Dispatcher::getInstance()->disableView();
        $names = [
            'fid'=>3,
            'title'=>6,
            'link'=>6,
        ];
        $data = Request::batch(1,$names);
        $model = new NavigationModel;
        $flag = $model->insert($data);
        if(empty($flag)){
            Response::error('error');
        }else{
            Response::success('nice job');
        }
    }
    public function editAction(){
        $id = Request::asget(3,'id',null);
        if($id == null){
            return false;
        }else{
            $where = ['id'=>$id];
            $model = new NavigationModel;
            $data = $model->select();
            $_nav = null;
            foreach($data as $key=>$v){
                if($v['id'] == $id){
                    $_nav = $v;
                    array_splice($data,$key,1);
                    break;
                }
            }
            $this->_view->nav = $_nav;
            $this->_view->navs = $data;
        }        
    }
    public function updateAction(){
        Yaf_Dispatcher::getInstance()->disableView();
        $id = Request::aspost(3,'id',0);
        if($id == 0){
            return false;
        }else{
            $data['fid'] = Request::aspost(3,'fid',0);
            $data['link'] = Request::aspost(6,'link','#');
            $data['title'] = Request::aspost(6,'title','#');
            $model = new NavigationModel;
            $flag = $model->where(['id'=>$id])->update($data);
            if(empty($flag)){
                Response::error('error');
            }else{
                Response::success('nice job');
            }
        }
    }
    public function statusAction(){
        Yaf_Dispatcher::getInstance()->disableView();
        $id = Request::aspost(3,'id','');
        if(empty($id)){
            die;
        }else{
            $status = isset($_POST['status']) ? $_POST['status'] : '';
            if($status == ''){
                return false;
            }else{
                
                switch($status){
                case '0': $data['status'] = 1;
                    break;
                case '1': $data['status'] = 0;
                    break;
                }
                $model = new NavigationModel;
                $flag = $model->where(['id'=>$id])->update($data);
                if($flag == 0 || $flag == false){
                    Response::error('');
                }else{
                    Response::success('nice');
                }
            }
        }        
    }    
    public function removeAction(){
        Yaf_Dispatcher::getInstance()->disableView();
        if($this->_request->isPost()){
            $id = Request::aspost(3,'id');
            if($id === null){
                exit;
            }else{
                $model = new NavigationModel;
                $flag = $model->delete($id);
                if($flag){
                    Response::success('Nice!');
                }else{
                    Response::error('Bad');
                }
            }
        }else{
            exit;
        }           
    }
}